package llc.ufwa.unlimitedlist.loader;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import llc.ufwa.data.exception.ResourceException;
import llc.ufwa.data.resource.loader.DefaultResourceLoader;
import llc.ufwa.unlimitedlist.R;
import llc.ufwa.unlimitedlist.data.UnlimitedListItem;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class RootLoader extends DefaultResourceLoader<Long, UnlimitedListItem> {

	private static final Logger logger = LoggerFactory.getLogger(RootLoader.class);
	
    private final Context context;
	
	public RootLoader(final Context context) {
	    this.context = context;
	}
	@Override
	public UnlimitedListItem get(Long key) throws ResourceException {
		
		logger.debug("Starting root loader: " + key);
		
		logger.debug("loaded " + key);
		
		try {
            return new UnlimitedListItem(key, getBitmapData(context));
        } 
		catch (InterruptedException e) {
            throw new ResourceException("ERROR", e);
        }
		
	}
	
	public static final int COLOR_MIN = 0x00;
	public static final int COLOR_MAX = 0xFF;

	public static Bitmap applyFleaEffect(Bitmap source) {
	    // get image size
	    int width = source.getWidth();
	    int height = source.getHeight();
	    int[] pixels = new int[width * height];
	    // get pixel array from source
	    source.getPixels(pixels, 0, width, 0, 0, width, height);
	    // a random object
	    Random random = new Random(System.currentTimeMillis());

	    int index = 0;
	    // iteration through pixels
	    for(int y = 0; y < height; ++y) {
	        for(int x = 0; x < width; ++x) {
	            // get current index in 2D-matrix
	            index = y * width + x;
	            // get random color
	            int randColor = Color.rgb(random.nextInt(COLOR_MAX),
	                    random.nextInt(COLOR_MAX), random.nextInt(COLOR_MAX));
	            // OR
	            pixels[index] = randColor;
	        }
	    }
	    // output bitmap
	    Bitmap bmOut = Bitmap.createBitmap(width, height, source.getConfig());
	    bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
	    return bmOut;
	}
	
	public static byte [] getBitmapData(final Context context) throws InterruptedException {
	    
        Random rm = new Random();
        Drawable drawable = context.getResources().getDrawable(R.drawable.image);
        Bitmap bitmap = applyFleaEffect(((BitmapDrawable) drawable).getBitmap());
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        byte[] buffer = out.toByteArray();
//       
//        Bitmap bmp = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
//        return bmp;
        
        return buffer;
        
    }   
	

	@Override
	public List<UnlimitedListItem> getAll(
        final List<Long> keys
    ) throws ResourceException {
		
		for(Long key : keys) {
			logger.debug("starting getAll: " + key);
		}
		
		final List<UnlimitedListItem> returnVals = new ArrayList<UnlimitedListItem>(keys.size());
		
		final byte[] data;
		
		try {
		    data = getBitmapData(context);
		} 
        catch (InterruptedException e) {
            throw new ResourceException("ERROR", e);
        }    
		
		for(final Long key : keys) {
			
            returnVals.add(new UnlimitedListItem(key, data));
            
			logger.debug("loaded: " + key);
		}
		
		return returnVals;
		
	}
	
	

}
